package com.eunion.web.cms.entity.spec;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.jpa.domain.Specification;

import com.eunion.web.cms.entity.MajorRecruitPlan;

public class MajorStudentsPlanSpec {
    public static Specification<MajorRecruitPlan> build(MajorRecruitPlan queryBean) {
        return new MajorStudentsPlanPlanQuery(queryBean);
    }
    
    public static Specification<MajorRecruitPlan> build(MajorRecruitPlan queryBean,boolean accurate) {
        return new MajorStudentsPlanPlanQuery(queryBean,accurate);
    }

    private static class MajorStudentsPlanPlanQuery implements Specification<MajorRecruitPlan> {

        private MajorRecruitPlan queryBean;
        private boolean accurate;

        private MajorStudentsPlanPlanQuery(MajorRecruitPlan queryBean) {
            this.queryBean = queryBean;
        }
        
        private MajorStudentsPlanPlanQuery(MajorRecruitPlan queryBean,boolean accurate) {
            this.queryBean = queryBean;
            this.accurate = accurate;
        }

        @Override
        public Predicate toPredicate(Root<MajorRecruitPlan> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            List<Predicate> list = getPredicates(root, query, cb, queryBean,accurate);
            Predicate[] preList = new Predicate[list.size()];
            return cb.and(list.toArray(preList));
        }

    }

    private static List<Predicate> getPredicates(Root<MajorRecruitPlan> root, CriteriaQuery<?> query, CriteriaBuilder cb,
            MajorRecruitPlan queryBean,boolean accurate) {
        // 条件集合
        List<Predicate> list = new ArrayList<>();
        
        //年份
        String year = queryBean.getYear();
        //省份
        String provinceName = queryBean.getProvinceName();
        //大学编码
        String collegeCode = queryBean.getCollegeCode();
        int batch = queryBean.getBatch();
        int direction = queryBean.getDirection();
        String majorCode = queryBean.getMajorCode();
        if(accurate) {
            if (StringUtils.isNotBlank(year)) {
                list.add(cb.equal(root.get("year"),year.trim()));
            }
            if (StringUtils.isNotBlank(provinceName)) {
                list.add(cb.equal(root.get("provinceName"),provinceName.trim()));
            }
            if (StringUtils.isNotBlank(collegeCode)) {
                list.add(cb.equal(root.get("collegeCode"),collegeCode.trim()));
            }
            if(direction != 0) {
                list.add(cb.equal(root.get("direction"), direction));
            }
            if (batch != 0) {
                list.add(cb.equal(root.get("batch"),batch));
            }
            if(StringUtils.isNotBlank(majorCode)) {
                list.add(cb.equal(root.get("majorCode"),majorCode.trim()));
            }
            return list;
        }
        

        return list;
    }
}
